import java.util.ArrayList;
import java.util.List;
class School {
    private final List<Department> departments = new ArrayList<>();
    private final List<Student> students = new ArrayList<>();
    private String name; // 补充学院名称属性
    public School(String name) {
        this.name = name;
    }
    // 添加系时，建立双向关联（系属于当前学院）
    public void addDepartment(Department department) {
        if (department != null && !departments.contains(department)) {
            departments.add(department);
            department.setSchool(this);
        }
    }
    // 添加学生时，建立双向关联（学生属于当前学院）
    public void addStudent(Student student) {
        if (student != null && !students.contains(student)) {
            students.add(student);
            student.setSchool(this);
        }
    }
    // 补充必要的getter方法
    public String getName() {
        return name;
    }
    public List<Department> getDepartments() {
        return new ArrayList<>(departments); // 返回副本，避免外部修改
    }}
class Department {
    private final List<Instructor> instructors = new ArrayList<>();
    private final List<Subject> subjects = new ArrayList<>();
    private String name; // 补充系名称属性
    private School school; // 关联所属学院

    public Department(String name) {
        this.name = name;
    }
    // 添加教师时，建立双向关联（教师属于当前系）
    public void addInstructor(Instructor instructor) {
        if (instructor != null && !instructors.contains(instructor)) {
            instructors.add(instructor);
            instructor.addDepartment(this);
        }
    }
    // 添加课程时，建立双向关联（课程属于当前系）
    public void addSubject(Subject subject) {
        if (subject != null && !subjects.contains(subject)) {
            subjects.add(subject);
            subject.setDepartment(this);
        }
    }
    // 补充getter和setter
    public String getName() {
        return name;
    }

    public void setSchool(School school) {
        this.school = school;
    }
    public School getSchool() {
        return school;
    }}
class Instructor {
    private final String name;
    private final List<Subject> subjects = new ArrayList<>();
    private final List<Department> departments = new ArrayList<>(); // 关联所属系
    public Instructor(String name) {
        this.name = name;
    }
    // 限制教师最多教授3门课程（添加约束）
    public void addSubject(Subject subject) {
        if (subject != null && !subjects.contains(subject) && subjects.size() < 3) {
            subjects.add(subject);
            subject.addInstructor(this); // 双向关联
        }
    }
    // 接收所属系的关联
    public void addDepartment(Department department) {
        if (department != null && !departments.contains(department)) {
            departments.add(department);
        }
    }
    public String getName() {
        return name;
    }}
class Student {
    private final List<Subject> subjects = new ArrayList<>();
    private String name; // 补充学生名称
    private String studentId; // 补充学号
    private School school; // 关联所属学院
    public Student(String name, String studentId) {
        this.name = name;
        this.studentId = studentId;
    }
    // 限制学生最多选5门课程（添加约束）
    public void addSubject(Subject subject) {
        if (subject != null && !subjects.contains(subject) && subjects.size() < 5) {
            subjects.add(subject);
            subject.addStudent(this); // 双向关联
        }
    }
    // 关联所属学院
    public void setSchool(School school) {
        this.school = school;
    }
    public String getName() {
        return name;
    }}
class Subject {
    private final String name;
    private Department department; // 关联所属系
    private final List<Instructor> instructors = new ArrayList<>(); // 关联授课教师
    private final List<Student> students = new ArrayList<>(); // 关联选课学生
    public Subject(String name) {
        this.name = name;
    }
    // 接收授课教师关联
    public void addInstructor(Instructor instructor) {
        if (instructor != null && !instructors.contains(instructor)) {
            instructors.add(instructor);
        }
    }
    // 接收选课学生关联
    public void addStudent(Student student) {
        if (student != null && !students.contains(student)) {
            students.add(student);
        }
    }
    // 设置所属系
    public void setDepartment(Department department) {
        this.department = department;
    }
    public String getName() {
        return name;
    }
    public Department getDepartment() {
        return department;
    }}

